/**
 * copyright lexio 2006
 * copyright geolabs 2006-2007
 * author: Gérald Fenoy
 *
 */
System.require("Controler");

SelectOptions=Class.create({
  display: document,

  addOption: function(){
      element=arguments[0];
      value=arguments[1];
      label=arguments[2];
      var initialeOption = System.document.createElement('option');
      try{
         initialeOption.setAttribute('value',value);
         initialeOption.appendChild(System.document.createTextNode(label));
      }
	catch(e){
	  initialeOption.value = value;
	  initialeOption.text = label;
	}
      element.appendChild(initialeOption);
    },

  setValues: function(){
      element=arguments[0];
      options=arguments[1];
      for(var i=0;i<options.length;i++){
	this.addOption(element,options[i][0],options[i][1]);
      }
    },

  fill: function(){
      element=arguments[0];
      options=arguments[1];
      for(i=0;i<options.length;i++){
	/**
	 * Get the current item's id and val 
	 */
	currId=options[i].getElementsByTagName('id')[0].firstChild.nodeValue;
	currVal=options[i].getElementsByTagName('value')[0].firstChild.nodeValue;	
	this.addOption(element,currId,currVal);
      }
    },

  fillWithObject: function(){
      element=arguments[0];
      options=arguments[1];
      for(i=0;i<options.length;i++){
	/**
	 * Get the current item's id and val 
	 */
	currId=options[i].id;
	currVal=options[i].value;
	this.addOption(element,currId,currVal);
      }
    },

  empty: function(){
      var element=arguments[0];
      var limit=0;
      if(arguments.length>1)
	limit=arguments[1];
      
      try{
	element.selectedIndex=limit;
      }
      catch(e){/*alert(e);*/}

      for(var i=element.options.length-1;i>=limit;i--)
	element.removeChild(element.options[i]);
    },

  resetSelected: function(){
      arguments[0].selectedIndex=0;
    }
  });

/**
 * SelectAutoOptions Class
 */ 
SelectAutoOptions=Controler.extend();

SelectAutoOptions.define({
  _init: function(){

      //this.display=((arguments.length>3)? eval(arguments[3]) : document);

      this.setName(arguments[0]);
      //alert(arguments.length);
      if(arguments.length>1)
	this.setInitialValues(arguments[1]);
      if(arguments.length>2)
	this.setDependent(arguments[2]);
      else	
	this.setDependent(null);
      
      this.request=null;
      
      if(System.debug){
	debug=System.document.createElement('div');
	debug.setAttribute("id",this.name+"SDebug");
	debug.appendChild(document.createTextNode("debug: \n"));
	root.appendChild(debug);
      }
      
    },

  setName: function(){
      this.name=arguments[0];
      this.root=System.document.getElementById(this.name);
    },

  setInitialValues: function(){
	this.initialOptions=arguments[0];
	SelectOptions.setValues(this.root,Array(this.initialOptions),System.document);
    },

  setDependent: function(){
      if(!arguments[0])
	this.hasDependents=false;
      else{
	this.hasDependents=true;
	this.dependent=arguments[0];
	this.dependentSelect=System.document.getElementById(this.dependent);
      }
    },

  setDependentValues: function(){
      this.dependentValues=arguments[0];
    },

  updateDependentValues: function(){
      if(this.root.selectedIndex>0){
	SelectOptions.empty(this.dependentSelect,1);
	SelectOptions.fillWithObject(this.dependentSelect,this.dependentValues[this.root.options[this.root.selectedIndex].value]);
      }
    },

  empty: function(){
      SelectOptions.empty(this.root);
      if(this.initialOptions)
	SelectOptions.setValues(this.root,Array(this.initialOptions));
    },

  displayStatus: function(){
      if(this.status)
	this.superCall("displayStatus");
      try{
	this.root.options[this.root.selectedIndex].innerHTML=this.readyStateMess[this.request.readyState];
      }catch(e){
	this.empty();
      }
    },

  newData: function() {
      this.empty();
      /**
       * Load the query result and then load all tuple which it contains 
       */
      var xmldoc = this.request.responseXML;
      this.request=null;
      var rnode = xmldoc.getElementsByTagName('tuple');
      
      /**
       * DEBUG OUTPUT !!
       */
      try{
	if(System.debug){
	  System.document.getElementById(nameSelect[id]+"SDebug").innerHTML+=" "+nameSelect[id];
	  System.document.getElementById(nameSelect[id]+"SDebug").innerHTML+=" OK<br/>ajout de "+rnode.length+" options<br />";
	 }
      }
      catch(e){
	alert("Le mode debug est activé mais aucun champ de sortie n'a été ddéfinit !");
      }
      
      if(xmldoc.getElementsByTagName('tuple')[0].getElementsByTagName('id')[0].firstChild.nodeValue == '!')
	SelectOptions.empty(this.root);
      SelectOptions.fill(this.root,xmldoc.getElementsByTagName('tuple'));
      
    }
  });
    

SelectWithDependent=SelectAutoOptions.extend();
SelectWithDependent.define({
    
  });
